package com.mdd.admin.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IGoodsService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.goods.GoodsCreateValidate;
import com.mdd.admin.validate.goods.GoodsSearchValidate;
import com.mdd.admin.validate.goods.GoodsUpdateValidate;
import com.mdd.admin.validate.goods.GoodsValidateDto;
import com.mdd.admin.vo.goods.GoodsCommonVo;
import com.mdd.admin.vo.goods.GoodsDetailVo;
import com.mdd.admin.vo.goods.GoodsListedVo;
import com.mdd.admin.vo.goods.GoodsPriceVo;
import com.mdd.admin.vo.goods.GoodsSkuVo;
import com.mdd.admin.vo.goods.SpecListVo;
import com.mdd.admin.vo.goods.SpecValueListVo;
import com.mdd.admin.vo.goods.SpecValueVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Shop;
import com.mdd.common.entity.coupon.Coupon;
import com.mdd.common.entity.distribution.DistributionGoods;
import com.mdd.common.entity.distribution.DistributionLevel;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.goods.GoodsCategory;
import com.mdd.common.entity.goods.GoodsCategoryIndex;
import com.mdd.common.entity.goods.GoodsImage;
import com.mdd.common.entity.goods.GoodsSku;
import com.mdd.common.entity.goods.GoodsSkuName;
import com.mdd.common.entity.goods.GoodsSkuValue;
import com.mdd.common.entity.seckill.SeckillGoods;
import com.mdd.common.enums.SeckillEnum;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.ShopMapper;
import com.mdd.common.mapper.coupon.CouponMapper;
import com.mdd.common.mapper.distribution.DistributionGoodsMapper;
import com.mdd.common.mapper.distribution.DistributionLevelMapper;
import com.mdd.common.mapper.goods.GoodsCategoryIndexMapper;
import com.mdd.common.mapper.goods.GoodsCategoryMapper;
import com.mdd.common.mapper.goods.GoodsImageMapper;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.goods.GoodsSkuMapper;
import com.mdd.common.mapper.goods.GoodsSkuNameMapper;
import com.mdd.common.mapper.goods.GoodsSkuValueMapper;
import com.mdd.common.mapper.seckill.SeckillGoodsMapper;
import com.mdd.common.mapper.system.SystemAuthAdminMapper;
import com.mdd.common.util.ConfigUtils;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.RequestUtils;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 商品管理服务实现类
 */
@Service
public class GoodsServiceImpI implements IGoodsService {

	@Resource
	GoodsMapper goodsMapper;

	@Resource
	GoodsImageMapper goodsImageMapper;

	@Resource
	GoodsCategoryMapper goodsCategoryMapper;

	@Resource
	GoodsCategoryIndexMapper goodsCategoryIndexMapper;

	@Resource
	GoodsSkuMapper goodsSkuMapper;

	@Resource
	GoodsSkuNameMapper goodsSkuNameMapper;

	@Resource
	GoodsSkuValueMapper goodsSkuValueMapper;

	@Resource
	SeckillGoodsMapper seckillGoodsMapper;

	@Resource
	DistributionGoodsMapper distributionGoodsMapper;

	@Resource
	SystemAuthAdminMapper systemAuthAdminMapper;

	@Resource
	ShopMapper shopMapper;

	@Resource
	CouponMapper couponMapper;

	@Resource
	DistributionLevelMapper distributionLevelMapper;
	


	@Override
	public PageResult<GoodsCommonVo> common(PageValidate pageValidate, GoodsSearchValidate goodsSearchValidate) {
		Integer page = pageValidate.getPageNo();
		Integer limit = pageValidate.getPageSize();

		// 基础搜索
		QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_delete", 0);
		queryWrapper.orderByDesc("id");
		goodsMapper.setSearch(queryWrapper, goodsSearchValidate, new String[]{
				"=:code:str",
				"=:name:str",
				"=:type@status:int"
		});

		// 分类搜索
		Integer cid = goodsSearchValidate.getCategoryId();
		if (StringUtils.isNotNull(cid) && cid > 0) {
			List<GoodsCategoryIndex> categoryIndexList = goodsCategoryIndexMapper.selectList(
					new QueryWrapper<GoodsCategoryIndex>()
					.eq("category_id", cid));

			List<Integer> categoryIds = new ArrayList<>();
			for (GoodsCategoryIndex goodsCategoryIndex : categoryIndexList) {
				categoryIds.add(goodsCategoryIndex.getGoodsId());
			}

			if (categoryIds.size() > 0) {
				queryWrapper.in("id", categoryIds);
			}
		}

		IPage<Goods> iPage = goodsMapper.selectPage(new Page<>(page, limit), queryWrapper);
		List<GoodsCommonVo> list = new LinkedList<>();
		for (Goods record : iPage.getRecords()) {
			GoodsCommonVo vo = new GoodsCommonVo();
			BeanUtils.copyProperties(record, vo);
			vo.setCategory("");
			vo.setStatusMsg(record.getStatus().equals(0) ? "已下架" : "销售中");
			vo.setImage(UrlUtils.toAbsoluteUrl(record.getImage()));
			vo.setCreateTime(TimeUtils.timestampToDate(record.getCreateTime()));
			if (record.getMinPrice().subtract(record.getMaxPrice()).abs().compareTo(BigDecimal.ZERO) >= 0) {
				vo.setSellPrice("￥" + record.getMinPrice() + " ~ " + record.getMaxPrice());
			}

			// 商品分类
			List<Integer> cidList = new LinkedList<>();
			List<GoodsCategoryIndex> categoryIndexList = goodsCategoryIndexMapper.selectList(
					new QueryWrapper<GoodsCategoryIndex>()
					.eq("goods_id", record.getId()));

			for (GoodsCategoryIndex cIndex : categoryIndexList) {
				cidList.add(cIndex.getCategoryId());
			}

			if (cidList.size() > 0) {
				List<String> categoryNames = new LinkedList<>();
				List<GoodsCategory> categoryList = goodsCategoryMapper.selectList(
						new QueryWrapper<GoodsCategory>()
						.in("id", cidList)
						.eq("is_delete", 0));
				for (GoodsCategory gc : categoryList) {
					categoryNames.add(gc.getName());
				}
				vo.setCategory(ListUtils.listToStringByStr(categoryNames, ","));
			}

			// 是否秒杀
			long currTime = System.currentTimeMillis() / 1000;
			long isSeckill = seckillGoodsMapper.selectJoinCount(new MPJQueryWrapper<SeckillGoods>()
					.eq("t.goods_id", record.getId())
					.ne("SA.status", SeckillEnum.STATUS_END.getCode())
					.ge("SA.end_time", currTime)
					.innerJoin("?_seckill_activity SA ON t.seckill_id=SA.id".replace("?_", GlobalConfig.tablePrefix)));

			vo.setIsSeckill(isSeckill > 0 ? 1 : 0);

			// 商品规格
			List<GoodsSkuVo> skuVos = new LinkedList<>();
			List<GoodsSku> goodsSkus = goodsSkuMapper.selectList(new QueryWrapper<GoodsSku>().eq("goods_id", vo.getId()));
			for (GoodsSku sku : goodsSkus) {
				GoodsSkuVo skuVo = new GoodsSkuVo();
				BeanUtils.copyProperties(sku, skuVo);
				skuVo.setImage(UrlUtils.toAbsoluteUrl(sku.getImage()));
				skuVos.add(skuVo);
			}

			vo.setSkuList(skuVos);
			list.add(vo);
		}

		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
	}

	/**
	 * 商品列表
	 *
	 * @author cjhao
	 * @param pageValidate 分页参数
	 * @param goodsSearchValidate 搜索参数
	 * @return PageResult<GoodsListVo>
	 */
	@Override
	public PageResult<GoodsListedVo> list(PageValidate pageValidate, GoodsSearchValidate goodsSearchValidate) {
		Integer page = pageValidate.getPageNo();
		Integer limit = pageValidate.getPageSize();
		Integer shopId = LikeAdminThreadLocal.getShopId();
		QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
		if(null != shopId && shopId > 1) {
			queryWrapper.eq("shop_id", shopId);
		}
		queryWrapper.eq("is_delete", 0)
		.orderByDesc(Arrays.asList("sort", "id"));


		// 商品code搜索
		if (StringUtils.isNotEmpty(goodsSearchValidate.getCode())) {
			queryWrapper.like("code", goodsSearchValidate.getCode());
		}
		if(null != goodsSearchValidate.getIsTop()) {
			queryWrapper.eq("is_top", goodsSearchValidate.getIsTop());
		}

		// 商品名称搜索
		if (StringUtils.isNotEmpty(goodsSearchValidate.getName())) {
			queryWrapper.like("name", goodsSearchValidate.getName());
		}

		// 商品分类搜索
		if (null != goodsSearchValidate.getCategoryId() && goodsSearchValidate.getCategoryId() > 0) {
			List<GoodsCategoryIndex> categoryIndexList = goodsCategoryIndexMapper.selectList(
					new QueryWrapper<GoodsCategoryIndex>()
					.eq("category_id", goodsSearchValidate.getCategoryId()));

			List<Integer> categoryIds = new ArrayList<>();
			for (GoodsCategoryIndex goodsCategoryIndex : categoryIndexList) {
				categoryIds.add(goodsCategoryIndex.getGoodsId());
			}

			if (categoryIds.size() > 0) {
				queryWrapper.in("id", categoryIds);
			}
		}

		// 按类型查找
		Map<String, Object> statistics = this.__statistics(queryWrapper.clone());
		Integer type = goodsSearchValidate.getType();
		if(StringUtils.isNotNull(type)){
			switch (type) {
			case 1:
				queryWrapper.eq("status", 1);
				queryWrapper.eq("examine", 1);
				break;
			case 2:
				queryWrapper.apply("stock_warning > total_stock");
				break;
			case 3:
				queryWrapper.eq("status", 0);
				queryWrapper.eq("examine", 1);
				break;
			case 4:
				queryWrapper.eq("examine", 0);
				break;
			case 5:
				queryWrapper.eq("examine", 2);
				break;
			}
		}

		IPage<Goods> iPage = goodsMapper.selectPage(new Page<>(page, limit), queryWrapper);
		List<GoodsListedVo> list = new LinkedList<>();
		for (Goods record : iPage.getRecords()) {
			GoodsListedVo goodsListedVo = new GoodsListedVo();
			BeanUtils.copyProperties(record, goodsListedVo);
			goodsListedVo.setImage(UrlUtils.toAbsoluteUrl(record.getImage()));
			goodsListedVo.setSellPrice(String.valueOf(record.getMinPrice()));
			goodsListedVo.setCreateTime(TimeUtils.timestampToDate(record.getCreateTime()));
			goodsListedVo.setExamine(record.getExamine());
			goodsListedVo.setExamine_time(record.getCreateTime() == null ? "" : TimeUtils.timestampToDate(record.getCreateTime()));

			if (!(record.getMaxPrice().compareTo(record.getMinPrice()) == 0) && record.getSpecType().equals(2)) {
				goodsListedVo.setSellPrice(record.getMinPrice() + "~" + record.getMaxPrice());
			}
			// 查询商户名称
			//            SystemAuthAdmin admin = systemAuthAdminMapper.selectOne(new LambdaQueryWrapper<SystemAuthAdmin>()-
			//            		.eq(SystemAuthAdmin::getId, record.getShopId()).select(SystemAuthAdmin::getId,SystemAuthAdmin::getNickname,SystemAuthAdmin::getUsername));
			//            if(null != admin) {
			//            	String name = admin.getNickname();
			//            	if(StringUtils.isEmpty(name)) {
			//            		name = admin.getUsername();
			//            	}
			//            	goodsListedVo.setNickname(name);
			//            }
			Shop shop = shopMapper.selectOne(new LambdaQueryWrapper<Shop>().eq(Shop::getId, record.getShopId()).select(Shop::getId,Shop::getName));
			if(null != shop && StringUtils.isNotEmpty(shop.getName())) {
				goodsListedVo.setNickname(shop.getName());
			}
			list.add(goodsListedVo);
		}

		//statistics.put("allNum", iPage.getTotal());
		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list, statistics);
	}

	/**
	 * 商品详情
	 *
	 * @author cjhao
	 * @param id 主键
	 * @return GoodsDetailVo
	 */
	@Override
	public GoodsDetailVo detail(Integer id) {
		// 基础信息
		Goods goods = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("id", id)
				.eq("is_delete", 0)
				.last("limit 1"));

		Assert.notNull(goods, "商品不存在了!");
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
		BeanUtils.copyProperties(goods,goodsDetailVo);

		// 轮播图片
		List<GoodsImage> goodsImages = goodsImageMapper.selectList(new QueryWrapper<GoodsImage>().eq("goods_id", id));
		List<String> goodsImage = new LinkedList<>();
		for (GoodsImage image : goodsImages) {
			goodsImage.add(UrlUtils.toAbsoluteUrl(image.getImage()));
		}
		goodsDetailVo.setGoodsImage(goodsImage);

		// 商品分类
		List<GoodsCategoryIndex> goodsCategoryIndexList = goodsCategoryIndexMapper.selectList(new QueryWrapper<GoodsCategoryIndex>().eq("goods_id",id));
		List<Integer> goodsCategory = new LinkedList<>();
		for (GoodsCategoryIndex goodsCategoryIndex : goodsCategoryIndexList) {
			goodsCategory.add(goodsCategoryIndex.getCategoryId());
		}
		goodsDetailVo.setCategoryId(goodsCategory);

		// 商品规格
		goodsDetailVo.setSpecValueList(goodsSkuMapper.selectList(new QueryWrapper<GoodsSku>().eq("goods_id", id)));

		// 商品SKU
		List<GoodsSkuName> goodsSkuNameList = goodsSkuNameMapper.selectList(new QueryWrapper<GoodsSkuName>().eq("goods_id", id));
		List<GoodsSkuValue> goodsSkuValueList = goodsSkuValueMapper.selectList(new QueryWrapper<GoodsSkuValue>().eq("goods_id", id));
		List<SpecValueVo> specValue = new ArrayList<>();
		for (GoodsSkuName goodsSkuName : goodsSkuNameList) {
			SpecValueVo specValueVo = new SpecValueVo();
			BeanUtils.copyProperties(goodsSkuName,specValueVo);
			List<SpecListVo> specListVos = new ArrayList<>();
			for (GoodsSkuValue goodsSkuValue : goodsSkuValueList) {
				if(goodsSkuValue.getSkuNameId().equals(goodsSkuName.getId())){
					SpecListVo specListVo = new SpecListVo();
					BeanUtils.copyProperties(goodsSkuValue,specListVo);
					specListVos.add(specListVo);
				}
			}
			specValueVo.setSpecList(specListVos);
			specValue.add(specValueVo);
		}

		goodsDetailVo.setSpecValue(specValue);

		// 转绝对路径
		String domain = RequestUtils.domain();
		String engine = ConfigUtils.get("storage", "default", "local");
		if (!engine.equals("") && !engine.equals("local")) {
			Map<String, String> mapStorage = ConfigUtils.getMap("storage", engine);
			domain = mapStorage.getOrDefault("domain", RequestUtils.domain());
		}
		String content = goods.getContent().replaceAll("##domain##", domain);
		goodsDetailVo.setContent(content);

		return goodsDetailVo;
	}

	/**
	 * 	获取商品销售价格
	 * @param goodsCreateValidate
	 * @return
	 */
	private BigDecimal __getGoodsPrice(GoodsValidateDto goodsCreateValidate) {
		Integer specType = goodsCreateValidate.getSpecType();
		for (GoodsSkuVo goodSkuVo : goodsCreateValidate.getSpecValueList()) {
			if (null == goodSkuVo.getPrice() || goodSkuVo.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
				throw new OperateException("销售价格不能少于等于0");
			}
			if (null == goodSkuVo.getStock() || goodSkuVo.getStock() < 0) {
				throw new OperateException("库存数量不能少于0");
			}
		}
		List<BigDecimal> priceList = new LinkedList<>();
		if (1 == specType) {
			return goodsCreateValidate.getSpecValueList().get(0).getPrice();
		} else {
			List<GoodsSkuVo> serviceSpecValueList = goodsCreateValidate.getSpecValueList();
			for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
				priceList.add(goodsSkuVo.getPrice());
			}
			return Collections.min(priceList);
		}
	}
	
	/**
	 * 	获取销售价格和成本价
	 * @param goodsCreateValidate
	 * @return
	 */
	private List<GoodsPriceVo> __getGoodsPriceAndMarketPrice(GoodsValidateDto goodsCreateValidate) {
		Integer specType = goodsCreateValidate.getSpecType();
		for (GoodsSkuVo goodSkuVo : goodsCreateValidate.getSpecValueList()) {
			if (null == goodSkuVo.getPrice() || goodSkuVo.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
				throw new OperateException("销售价格不能少于等于0:["+goodSkuVo.getSkuValueArr()+"]");
			}
			if (null == goodSkuVo.getStock() || goodSkuVo.getStock() < 0) {
				throw new OperateException("库存数量不能少于0:["+goodSkuVo.getSkuValueArr()+"]");
			}
			if(null == goodSkuVo.getMarketPrice() || goodSkuVo.getMarketPrice().compareTo(BigDecimal.ZERO) <= 0) {
				throw new OperateException("供货价不能少于等于0:["+goodSkuVo.getSkuValueArr()+"]");
			}
			if(goodSkuVo.getMarketPrice().compareTo(goodSkuVo.getPrice()) > 0) {
				throw new OperateException("产品供货价不可大于售价:["+goodSkuVo.getSkuValueArr()+"]");
			}
		}
		List<GoodsPriceVo> list = new LinkedList<>();
		if (1 == specType) {
			GoodsPriceVo vo = new GoodsPriceVo();
			vo.setMarketPrice(goodsCreateValidate.getSpecValueList().get(0).getMarketPrice());
			vo.setPrice(goodsCreateValidate.getSpecValueList().get(0).getPrice());
			vo.setSkuValueArr(goodsCreateValidate.getSpecValueList().get(0).getSkuValueArr());
		} else {
			List<GoodsSkuVo> serviceSpecValueList = goodsCreateValidate.getSpecValueList();
			for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
				GoodsPriceVo vo = new GoodsPriceVo();
				BeanUtils.copyProperties(goodsSkuVo, vo);
				list.add(vo);
			}
		}
		return list;
	}

	/**
	 * 	校验返现金额
	 * @param createValidate
	 * @param type				1=添加 2=修改
	 * @param goodsSpec			为2时 使用 原有的商品规格类型
	 */
//	private void __checkCashbackPrice(GoodsValidateDto createValidate) {
//		// 下单再返金额设置规则：【产品售价-供货价】*50%＞钻石会员返还金额≥金牌会员返还金额≥银牌会员返还金额≥普通用户返还金额，不符合此规则设置无效
//		if(createValidate.getSupplyPrice().compareTo(BigDecimal.ZERO) <= 0) {
//			Assert.notNull(null, "请输入供货价");
//		}
//		Assert.isTrue(createValidate.getCashbackDiamond().compareTo(createValidate.getCashbackGold()) >= 0 
//				&& createValidate.getCashbackGold().compareTo(createValidate.getCashbackSilver()) >=0
//				&& createValidate.getCashbackSilver().compareTo(createValidate.getCashbackOrdinary()) >=0, "钻石会员返还金额≥金牌会员返还金额≥银牌会员返还金额≥普通用户返还金额");
//		BigDecimal goodsPrice = __getGoodsPrice(createValidate);
//		BigDecimal subtract = goodsPrice.subtract(createValidate.getSupplyPrice());
//		BigDecimal multiply = subtract.multiply(new BigDecimal("0.5"));
//		if(multiply.compareTo(createValidate.getCashbackDiamond()) <= 0) {
//			throw new OperateException("【产品售价("+goodsPrice+") - 供货价("+createValidate.getSupplyPrice()+")】*50% 需大于钻石会员返还金额");
//		}
//		//		throw new OperateException("测试正常");
//		if(1 == createValidate.getIsCashback()) {
//			Assert.isTrue(__checkIsExchange(goodsPrice, createValidate), "当前商品不支持返现购物");
//		}
//	}

	/**
	 * 	校验商品是否符合积分兑换要求
	 * @param goodsPrice
	 * @param createValidate
	 * @return
	 */
	private boolean __checkIsExchange(BigDecimal goodsPrice,GoodsValidateDto createValidate) {
		// 优惠金额 = 10
		BigDecimal preferentialPrice = BigDecimal.ZERO;
		List<BigDecimal> priceList = new LinkedList<>();
		priceList.add(BigDecimal.ZERO);
		List<Coupon> list = couponMapper.selectList(new LambdaQueryWrapper<Coupon>().eq(Coupon::getIsDelete, 0).eq(Coupon::getStatus, 2));
		if(CollectionUtil.isNotEmpty(list)) {
			for (Coupon coup : list) {
				if(2 == coup.getSendTotalType() && coup.getSendTotal() <= 0) {
					continue;
				}
				if(2 == coup.getConditionType() && coup.getConditionMoney().compareTo(goodsPrice) < 0) {
					continue;
				}
				if(StringUtils.isNotEmpty(coup.getUseGoodsIds())) {
					if(2 == coup.getUseGoodsType() && !coup.getUseGoodsIds().contains(String.valueOf(createValidate.getId()))) {
						continue;
					}else if(3 == coup.getUseGoodsType() && coup.getUseGoodsIds().contains(String.valueOf(createValidate.getId()))) {
						continue;
					}
				}
				priceList.add(coup.getMoney());
			}
		}
		// 优惠券金额
		preferentialPrice = Collections.max(priceList);

		// 【售价-钻石会员下单再返金额-供货价-领券优惠金额】/【售价-钻石会员下单再返金额】*100%≥40%，
		//	默认参与返现购物【可取消勾选，提示：参与返现购物有利于提升产品销量，费用由平台向商户结算，结算价不变】，利润低于40%不支持积分兑换【灰色不可选】。
		BigDecimal leftPrice = goodsPrice.subtract(createValidate.getCashbackDiamond()).subtract(createValidate.getSupplyPrice()).subtract(preferentialPrice);
		BigDecimal rightPrice = goodsPrice.subtract(createValidate.getCashbackDiamond());
		BigDecimal setScale = leftPrice.divide(rightPrice,2, RoundingMode.DOWN);
		return setScale.compareTo(new BigDecimal("0.4")) >= 0;
	}

	/**
	 * 	校验价格
	 */
	public void checkPriceIsError(GoodsUpdateValidate dto) {
		GoodsValidateDto dto1 = new GoodsValidateDto();
		BeanUtils.copyProperties(dto, dto1);
		__getGoodsPriceAndMarketPrice(dto1);
	}
	/**
	 * 商品新增
	 *
	 * @author cjhao
	 * @param createValidate 参数
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(GoodsCreateValidate createValidate) {
		Integer shopId = LikeAdminThreadLocal.getShopId();
		// 笛卡尔积规格
		createValidate.setServiceSpecValueList(this.__cartesianSpec(createValidate));

		// 验证商品数据
		this.__checkAddGoodsData(createValidate);

		// 转相对路径
		String domain = RequestUtils.domain();
		String engine = ConfigUtils.get("storage", "default", "local");
		if (!engine.equals("local")) {
			Map<String, String> mapStorage = ConfigUtils.getMap("storage", engine);
			domain = mapStorage.getOrDefault("domain", RequestUtils.domain());
		}
		String content = createValidate.getContent().replaceAll(domain, "##domain##");
		// 判断下单再返是否符合逻辑
		GoodsUpdateValidate dto = new GoodsUpdateValidate();
		BeanUtils.copyProperties(createValidate, dto);
		checkPriceIsError(dto);
		if(dto.getCommissionType() == 1 || dto.getCommissionType() == 2) {
			String checkPrice = this.checkPrice(dto);
			if(!"分佣合理".equals(checkPrice)) {
				throw new OperateException(checkPrice);
			}
		}
		
//		this.__checkCashbackPrice(dto);

		// 添加商品信息
		Goods goods = new Goods();
		goods.setCode(createValidate.getCode());
		goods.setName(createValidate.getName());
		goods.setStatus(createValidate.getStatus());
		goods.setDescription(createValidate.getDescription());
		goods.setVideoStatus(createValidate.getVideoStatus());
		goods.setVideoSource(createValidate.getVideoSource());
		goods.setVideoCover(createValidate.getVideoCover());
		goods.setVideo(createValidate.getVideo());
		goods.setImage(UrlUtils.toRelativeUrl(createValidate.getGoodsImage().get(0)));
		goods.setExpressType(createValidate.getExpressType());
		goods.setExpressTemplateId(createValidate.getExpressTemplateId());
		goods.setVirtualSalesNum(createValidate.getVirtualSalesNum() < 0 ? 0 : createValidate.getVirtualSalesNum());
		goods.setVirtualClickNum(createValidate.getVirtualClickNum() < 0 ? 0 : createValidate.getVirtualClickNum());
		goods.setStockWarning(createValidate.getStockWarning());
		goods.setSpecType(createValidate.getSpecType());
		goods.setSort(createValidate.getSort() < 0 ? 0 : createValidate.getSort());
		goods.setContent(content);
		goods.setCreateTime(System.currentTimeMillis() / 1000);
		goods.setUpdateTime(System.currentTimeMillis() / 1000);
		goods.setIsTop(createValidate.getIsTop());
		if(null != createValidate.getShopId() && createValidate.getShopId() > 0) {
			shopId = createValidate.getShopId();
		}
		goods.setShopId(shopId);
		goods.setSupplyPrice(createValidate.getSupplyPrice());
		goods.setCashbackOrdinary(createValidate.getCashbackOrdinary());
		goods.setCashbackSilver(createValidate.getCashbackSilver());
		goods.setCashbackGold(createValidate.getCashbackGold());
		goods.setCashbackDiamond(createValidate.getCashbackDiamond());
		goods.setCashbackPartner(createValidate.getCashbackPartner());
		goods.setCashbackCreate8(createValidate.getCashbackCreate8());
		goods.setCashbackCreate9(createValidate.getCashbackCreate9());
		goods.setIsCashback(createValidate.getIsCashback());
		goods.setReceiveVip(createValidate.getReceiveVip());
		goods.setCommissionType(createValidate.getCommissionType());
		goods.setCommissionRatio(createValidate.getCommissionRatio());
		goods.setLevel1Silver(createValidate.getLevel1Silver());
		goods.setLevel2Silver(createValidate.getLevel2Silver());
		goods.setLevel1Gold(createValidate.getLevel1Gold());
		goods.setLevel2Gold(createValidate.getLevel2Gold());
		goods.setLevel1Diamond(createValidate.getLevel1Diamond());
		goods.setLevel2Diamond(createValidate.getLevel2Diamond());
		goods.setLevel2DiamondGold(createValidate.getLevel2DiamondGold());
		goods.setCashbackRatio(createValidate.getCashbackRatio());
		goods.setGoldCoin(createValidate.getGoldCoin());
		goods.setCommissionRatio(createValidate.getCommissionRatio());
		
		
		goods.setLevel1Partner(createValidate.getLevel1Diamond());
		goods.setLevel2Partner(createValidate.getLevel2Diamond());
		goods.setLevel2PartnerGold(createValidate.getLevel2DiamondGold());
		
		goods.setLevel1Create8(createValidate.getLevel1Diamond());
		goods.setLevel2Create8(createValidate.getLevel2Diamond());
		goods.setLevel2Create8Gold(createValidate.getLevel2DiamondGold());
		
		goods.setLevel1Create9(createValidate.getLevel1Diamond());
		goods.setLevel2Create9(createValidate.getLevel2Diamond());
		goods.setLevel2Create9Gold(createValidate.getLevel2DiamondGold());
		goods.setCity(createValidate.getCity());
		goods.setParameter(createValidate.getParameter());
		goodsMapper.insert(goods);
		// 添加商品规格
		Map<String, Object> goodsSkuData = this.__addGoodsSpec(goods.getId(), createValidate);
		goods.setMinPrice((BigDecimal) goodsSkuData.get("minPrice"));
		goods.setMaxPrice((BigDecimal) goodsSkuData.get("maxPrice"));
		goods.setMinLineationPrice((BigDecimal) goodsSkuData.get("minLineationPrice"));
		goods.setMaxLineationPrice((BigDecimal) goodsSkuData.get("maxLineationPrice"));
		goods.setMaxLineationPrice((BigDecimal) goodsSkuData.get("maxLineationPrice"));
		goods.setTotalStock((Integer) goodsSkuData.get("totalStock"));
		goodsMapper.updateById(goods);

		// 添加商品图片
		for (String image : createValidate.getGoodsImage()) {
			GoodsImage goodsImage = new GoodsImage();
			goodsImage.setGoodsId(goods.getId());
			goodsImage.setImage(UrlUtils.toRelativeUrl(image));
			goodsImageMapper.insert(goodsImage);
		}

		// 添加商品分类
		for (Integer categoryId : createValidate.getCategoryId()) {
			GoodsCategoryIndex goodsCategoryIndex = new GoodsCategoryIndex();
			goodsCategoryIndex.setGoodsId(goods.getId());
			goodsCategoryIndex.setCategoryId(categoryId);
			goodsCategoryIndexMapper.insert(goodsCategoryIndex);
		}
	}

	/**
	 * 商品编辑
	 *
	 * @author cjhao
	 * @param updateValidate 参数
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean edit(GoodsUpdateValidate updateValidate) {
		// 查询商品信息
		Goods goods = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("id", updateValidate.getId())
				.eq("is_delete", 0));

		// 验证商品存在
		Assert.notNull(goods,"商品不存在了!");

		// 验证是否为营销活动中的商品
		if (goods.getIsActivity() == 1){
			throw new OperateException("商品正在参加活动中，不允许修改！");
		}

		// 旧的规格类型
		Integer goodsSpec = goods.getSpecType();

		// 转相对路径
		String engine = ConfigUtils.get("storage", "default", "local");
		String domain = RequestUtils.domain();
		if (!engine.equals("local")) {
			Map<String, String> mapStorage = ConfigUtils.getMap("storage", engine);
			domain = mapStorage.getOrDefault("domain", RequestUtils.domain());
		}
		String content = updateValidate.getContent().replaceAll(domain, "##domain##");

		// 判断下单再返是否符合逻辑
//		GoodsValidateDto dto = new GoodsValidateDto();
//		BeanUtils.copyProperties(updateValidate, dto);
//		this.__checkCashbackPrice(dto);
		checkPriceIsError(updateValidate);
		if(updateValidate.getCommissionType() == 1 || updateValidate.getCommissionType() == 2) {
			String checkPrice = this.checkPrice(updateValidate);
			if(!"分佣合理".equals(checkPrice)) {
				throw new OperateException(checkPrice);
			}
		}
		// 更新商品信息
		goods.setName(updateValidate.getName());
		goods.setCode(updateValidate.getCode());
		goods.setDescription(updateValidate.getDescription());
		goods.setVideoStatus(updateValidate.getVideoStatus());
		goods.setVideoSource(updateValidate.getVideoSource());
		goods.setVideoCover(updateValidate.getVideoCover());
		goods.setVideo(updateValidate.getVideo());
		if(goods.getStatus() == 1) {
			// 审核状态 0：待审核 1：审核通过 2：审核失败
			goods.setExamine(0);
		}
		goods.setStatus(0);
		goods.setImage(UrlUtils.toRelativeUrl(updateValidate.getGoodsImage().get(0)));
		goods.setExpressTemplateId(updateValidate.getExpressTemplateId());
		goods.setExpressType(updateValidate.getExpressType());
		goods.setVirtualSalesNum(updateValidate.getVirtualSalesNum() < 0 ? 0 : updateValidate.getVirtualSalesNum());
		goods.setVirtualClickNum(updateValidate.getVirtualClickNum() < 0 ? 0 : updateValidate.getVirtualClickNum());
		goods.setStockWarning(updateValidate.getStockWarning());
		goods.setSpecType(updateValidate.getSpecType());
		goods.setContent(content);
		goods.setSort(updateValidate.getSort() < 0 ? 0 :updateValidate.getSort());
		goods.setUpdateTime(System.currentTimeMillis() / 1000);
		goods.setIsTop(updateValidate.getIsTop());
		goods.setSupplyPrice(updateValidate.getSupplyPrice());
		goods.setCashbackOrdinary(updateValidate.getCashbackOrdinary());
		goods.setCashbackSilver(updateValidate.getCashbackSilver());
		goods.setCashbackGold(updateValidate.getCashbackGold());
		goods.setCashbackDiamond(updateValidate.getCashbackDiamond());
		goods.setCashbackPartner(updateValidate.getCashbackPartner());
		goods.setCashbackCreate8(updateValidate.getCashbackCreate8());
		goods.setCashbackCreate9(updateValidate.getCashbackCreate9());
		goods.setIsCashback(updateValidate.getIsCashback());
		goods.setReceiveVip(updateValidate.getReceiveVip());
		goods.setCommissionType(updateValidate.getCommissionType());
		goods.setCommissionRatio(updateValidate.getCommissionRatio());
		goods.setLevel1Silver(updateValidate.getLevel1Silver());
		goods.setLevel2Silver(updateValidate.getLevel2Silver());
		goods.setLevel1Gold(updateValidate.getLevel1Gold());
		goods.setLevel2Gold(updateValidate.getLevel2Gold());
		goods.setLevel1Diamond(updateValidate.getLevel1Diamond());
		goods.setLevel2Diamond(updateValidate.getLevel2Diamond());
		goods.setLevel2DiamondGold(updateValidate.getLevel2DiamondGold());
		goods.setCashbackRatio(updateValidate.getCashbackRatio());
		goods.setGoldCoin(updateValidate.getGoldCoin());
		goods.setCashbackRatio(updateValidate.getCashbackRatio());
		
		goods.setLevel1Partner(updateValidate.getLevel1Diamond());
		goods.setLevel2Partner(updateValidate.getLevel2Diamond());
		goods.setLevel2PartnerGold(updateValidate.getLevel2DiamondGold());
		
		goods.setLevel1Create8(updateValidate.getLevel1Diamond());
		goods.setLevel2Create8(updateValidate.getLevel2Diamond());
		goods.setLevel2Create8Gold(updateValidate.getLevel2DiamondGold());
		
		goods.setLevel1Create9(updateValidate.getLevel1Diamond());
		goods.setLevel2Create9(updateValidate.getLevel2Diamond());
		goods.setLevel2Create9Gold(updateValidate.getLevel2DiamondGold());
		goods.setCity(updateValidate.getCity());
		goods.setParameter(updateValidate.getParameter());
		goodsMapper.updateById(goods);

		// 更新商品分类
		goodsCategoryIndexMapper.delete(new QueryWrapper<GoodsCategoryIndex>().eq("goods_id", goods.getId()));
		for (Integer categoryId : updateValidate.getCategoryId()) {
			GoodsCategoryIndex goodsCategoryIndex = new GoodsCategoryIndex();
			goodsCategoryIndex.setGoodsId(goods.getId());
			goodsCategoryIndex.setCategoryId(categoryId);
			goodsCategoryIndexMapper.insert(goodsCategoryIndex);
		}

		// 删除商品轮播
		goodsImageMapper.delete(new QueryWrapper<GoodsImage>().eq("goods_id", goods.getId()));
		for (String image : updateValidate.getGoodsImage()) {
			GoodsImage goodsImage = new GoodsImage();
			goodsImage.setGoodsId(goods.getId());
			goodsImage.setImage(UrlUtils.toRelativeUrl(image));
			goodsImageMapper.insert(goodsImage);
		}

		// 处理商品规格
		Map<String, Object> goodsSkuData = this.__editGoodsSpec(goodsSpec, updateValidate);
		goods.setMaxPrice((BigDecimal) goodsSkuData.get("maxPrice"));
		goods.setMinPrice((BigDecimal) goodsSkuData.get("minPrice"));
		goods.setMinLineationPrice((BigDecimal) goodsSkuData.get("minLineationPrice"));
		goods.setMaxLineationPrice((BigDecimal) goodsSkuData.get("maxLineationPrice"));
		goods.setMaxLineationPrice((BigDecimal) goodsSkuData.get("maxLineationPrice"));
		goods.setTotalStock((Integer) goodsSkuData.get("totalStock"));
		goodsMapper.updateById(goods);

		// 验证是否为分销商品
		DistributionGoods distributionGoods = distributionGoodsMapper.selectOne(new QueryWrapper<DistributionGoods>()
				.eq("goods_id", goods.getId())
				.eq("is_delete", 0)
				.last("limit 1"));

		if (distributionGoods != null){
			distributionGoods.setIsDistribution(0);
			distributionGoods.setRule(1);
			distributionGoods.setUpdateTime(System.currentTimeMillis() / 1000);
			return false;
		}
		return true;
	}

	/**
	 * 商品删除
	 *
	 * @author cjhao
	 * @param id 商品ID
	 */
	@Override
	public void del(Integer id) {
		Goods goods = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("id", id)
				.eq("is_delete", 0));

		Assert.notNull(goods,"商品不存在了!");

		// 验证是否为营销活动中的商品
		if (goods.getIsActivity() == 1){
			throw new OperateException("商品正在参加活动中，不允许删除！");
		}

		goods.setIsDelete(1);
		goods.setUpdateTime(TimeUtils.timestamp());
		goodsMapper.updateById(goods);
	}

	/**
	 * 商品状态
	 *
	 * @author cjhao
	 * @param id 商品ID
	 */
	public void change(Integer id){
		Goods goods = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("id", id)
				.eq("is_delete", 0));

		Assert.notNull(goods,"商品不存在了!");

		// 验证是否为营销活动中的商品
		if (goods.getIsActivity() == 1){
			throw new OperateException("商品正在参加活动中，不允许修改状态！");
		}

		goods.setStatus(goods.getStatus()==0?1:0);
		goods.setUpdateTime(TimeUtils.timestamp());
		goodsMapper.updateById(goods);
	}

	/**
	 * 商品排序
	 *
	 * @author fzr
	 * @param id 主键
	 * @param sort 排序
	 */
	@Override
	public void sort(Integer id, Integer sort) {
		if (sort < 0) {
			throw new OperateException("排序值需大于0");
		}

		Goods goods = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("id", id)
				.eq("is_delete", 0));

		Assert.notNull(goods,"商品不存在了!");

		goods.setSort(sort);
		goods.setUpdateTime(TimeUtils.timestamp());
		goodsMapper.updateById(goods);
	}

	/**
	 * 批量删除
	 *
	 * @author fzr
	 * @param ids 主键集
	 */
	@Override
	public void batchDelete(List<Integer> ids) {
		List<Goods> goodsList = goodsMapper.selectList(new QueryWrapper<Goods>()
				.in("id", ids)
				.eq("is_delete", 0));

		for (Goods goods : goodsList) {
			goods.setStatus(0);
			goods.setIsDelete(1);
			goods.setUpdateTime(System.currentTimeMillis() / 1000);
			goodsMapper.updateById(goods);
		}
	}

	/**
	 * 批量上架
	 *
	 * @author fzr
	 * @param ids 主键集
	 */
	@Override
	public void batchUpper(List<Integer> ids) {
		List<Goods> goodsList = goodsMapper.selectList(new QueryWrapper<Goods>()
				.in("id", ids)
				.eq("is_delete", 0));

		for (Goods goods : goodsList) {
			goods.setStatus(1);
			goods.setUpdateTime(System.currentTimeMillis() / 1000);
			goodsMapper.updateById(goods);
		}
	}

	/**
	 * 批量下架
	 *
	 * @author fzr
	 * @param ids 主键集
	 */
	@Override
	public void batchLower(List<Integer> ids) {
		List<Goods> goodsList = goodsMapper.selectList(new QueryWrapper<Goods>()
				.in("id", ids)
				.eq("is_delete", 0));

		for (Goods goods : goodsList) {
			goods.setStatus(0);
			goods.setUpdateTime(System.currentTimeMillis() / 1000);
			goodsMapper.updateById(goods);
		}
	}

	/**
	 * 统计商品
	 *
	 * @author cjhao
	 * @param queryWrapper 搜索器
	 * @return Map<Object>
	 */
	private Map<String,Object> __statistics(QueryWrapper<Goods> queryWrapper) {
		Map<String,Object> statistics = new LinkedHashMap<>();

		//全部
		QueryWrapper<Goods> allNumWrapper = queryWrapper.clone();
		Long allNum = goodsMapper.selectCount(allNumWrapper.eq("is_delete", 0));
		// 销售中的
		QueryWrapper<Goods> salesNumWrapper = queryWrapper.clone();
		Long salesNum = goodsMapper.selectCount(salesNumWrapper.eq("status", 1).eq("examine",1));
		// 库存预警
		QueryWrapper<Goods> warningNumWrapper = queryWrapper.clone();
		Long warningNum = goodsMapper.selectCount(warningNumWrapper.apply("stock_warning > total_stock"));
		// 仓库中的
		QueryWrapper<Goods> storageNumWrapper = queryWrapper.clone();
		Long storageNum = goodsMapper.selectCount(storageNumWrapper.eq("status", 0).eq("examine",1));
		//审核中
		QueryWrapper<Goods> examineNumWrapper = queryWrapper.clone();
		Long examineNum = goodsMapper.selectCount(examineNumWrapper.eq("examine", 0));
		//审核失败
		QueryWrapper<Goods> examineNotNumWrapper = queryWrapper.clone();
		Long examineNotNum = goodsMapper.selectCount(examineNotNumWrapper.eq("examine", 2));
		// 返回数据
		statistics.put("salesNum",salesNum);
		statistics.put("warningNum",warningNum);
		statistics.put("storageNum",storageNum);
		statistics.put("examineNum",examineNum);
		statistics.put("allNum", allNum);
		statistics.put("examineNotNum", examineNotNum);

		return statistics;

	}

	/**
	 * 处理商品规格(创建)
	 *
	 * @author cjhao
	 * @param goodsCreateValidate 参数
	 * @return List<GoodsSkuVo>
	 */
	private List<GoodsSkuVo> __cartesianSpec(GoodsCreateValidate goodsCreateValidate) {
		List<SpecValueVo> specValue = goodsCreateValidate.getSpecValue();
		List<List<String>> specLists = new ArrayList<>();
		List<GoodsSkuVo> specValueList = goodsCreateValidate.getSpecValueList();
		if (specValueList.size() == 1) {
			return specValueList;
		}

		for (SpecValueVo specValueVo : specValue) {
			List<SpecListVo> specList = specValueVo.getSpecList();
			List<String> specName = new ArrayList<>();
			for (SpecListVo specListVo : specList) {
				specName.add(specListVo.getValue());
			}
			specLists.add(specName);
		}

		List<String> firstList = specLists.get(0);
		specLists.remove(0);
		List<SpecValueListVo> VoLists = new ArrayList<>();
		int firstKey = 0;
		for (String first : firstList) {
			SpecValueListVo specValueListVo = new SpecValueListVo();
			specValueListVo.setIds(Integer.toString(firstKey));
			specValueListVo.setValue(first);
			firstKey++;
			VoLists.add(specValueListVo);
		}

		for (List<String> strings : specLists) {
			List<SpecValueListVo> list = new ArrayList<>();
			for (SpecValueListVo voList : VoLists) {
				int specKey = 0;
				for (String spec : strings) {
					SpecValueListVo specValueListVo = new SpecValueListVo();
					specValueListVo.setValue(voList.getValue() + "," + spec);
					specValueListVo.setIds(voList.getIds() + "," + specKey);
					list.add(specValueListVo);
					specKey++;
				}
			}
			VoLists = list;
		}

		List<GoodsSkuVo> serviceSpecGoods = new ArrayList<>();
		for (SpecValueListVo voList : VoLists) {
			String ids = "";
			for (GoodsSkuVo goodsSku : specValueList) {
				if (goodsSku.getIds().equals(voList.getIds())) {
					ids = goodsSku.getIds();
					serviceSpecGoods.add(goodsSku);
				}
			}
			if (ids.equals("")) {
				throw new OperateException("商品规格信息错误!");
			}

		}

		return serviceSpecGoods;
	}

	/**
	 * 处理商品规格(更新)
	 *
	 * @author cjhao
	 * @param goodsUpdateValidate 参数
	 * @return List<GoodsSkuVo>
	 */
	public List<GoodsSkuVo> __cartesianSpec(GoodsUpdateValidate goodsUpdateValidate){
		List<List<String>> specLists   = new ArrayList<>();
		List<SpecValueVo> specValue    = goodsUpdateValidate.getSpecValue();
		List<GoodsSkuVo> specValueList = goodsUpdateValidate.getSpecValueList();

		if (1 == specValueList.size()) {
			return specValueList;
		}

		for (SpecValueVo specValueVo : specValue) {
			List<String> specName = new ArrayList<>();
			List<SpecListVo> specList = specValueVo.getSpecList();
			for (SpecListVo specListVo : specList) {
				specName.add(specListVo.getValue());
			}
			specLists.add(specName);
		}

		List<SpecValueListVo> VoLists = new ArrayList<>();
		List<String> firstList = specLists.get(0);
		specLists.remove(0);
		int firstKey = 0;
		for (String first : firstList) {
			SpecValueListVo specValueListVo = new SpecValueListVo();
			specValueListVo.setValue(first);
			specValueListVo.setIds(Integer.toString(firstKey));
			firstKey++;
			VoLists.add(specValueListVo);
		}

		for (List<String> strings : specLists) {
			List<SpecValueListVo> list = new ArrayList<>();
			for (SpecValueListVo voList : VoLists) {
				int specKey = 0;
				for (String spec : strings) {
					SpecValueListVo specValueListVo = new SpecValueListVo();
					specValueListVo.setIds(voList.getIds() + "," + specKey);
					specValueListVo.setValue(voList.getValue() + "," + spec);
					list.add(specValueListVo);
					specKey++;
				}

			}
			VoLists = list;
		}

		List<GoodsSkuVo> serviceSpecGoods = new ArrayList<>();
		for (SpecValueListVo voList : VoLists) {
			String ids = "";
			for (GoodsSkuVo goodsSku : specValueList) {
				if (goodsSku.getIds().equals(voList.getIds())) {
					ids = goodsSku.getIds();
					serviceSpecGoods.add(goodsSku);
				}
			}
			if (ids.equals("")) {
				throw new OperateException("商品规格信息错误");
			}
		}

		return serviceSpecGoods;
	}

	/**
	 * 添加商品规格
	 *
	 * @author cjhao
	 * @param goodsId 商品ID
	 * @param goodsCreateValidate 参数
	 * @return Map<Object>
	 */
	private Map<String, Object> __addGoodsSpec(Integer goodsId, GoodsCreateValidate goodsCreateValidate) {
		Integer specType = goodsCreateValidate.getSpecType();
		Map<String, Object> goodsSkuData = new LinkedHashMap<>();

		for (GoodsSkuVo goodSkuVo : goodsCreateValidate.getSpecValueList()) {
			if (goodSkuVo.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
				throw new OperateException("销售价格不能少于等于0");
			}
			if (goodSkuVo.getStock() < 0) {
				throw new OperateException("库存数量不能少于0");
			}
		}

		//单规格
		if (1 == specType) {
			//规格名
			GoodsSkuName goodsSkuName = new GoodsSkuName();
			goodsSkuName.setGoodsId(goodsId);
			goodsSkuName.setName("默认");
			goodsSkuNameMapper.insert(goodsSkuName);
			//规格值
			GoodsSkuValue goodsSkuValue = new GoodsSkuValue();
			goodsSkuValue.setGoodsId(goodsId);
			goodsSkuValue.setValue("默认");
			goodsSkuValue.setSkuNameId(goodsSkuName.getId());
			goodsSkuValueMapper.insert(goodsSkuValue);
			//规格项
			GoodsSku goodsSku = new GoodsSku();
			BeanUtils.copyProperties(goodsCreateValidate.getSpecValueList().get(0), goodsSku);
			goodsSku.setGoodsId(goodsId);
			goodsSku.setSkuValueIds(goodsSkuValue.getId().toString());
			goodsSku.setSkuValueArr("默认");
			goodsSku.setImage(UrlUtils.toRelativeUrl(goodsSku.getImage()));
			goodsSkuMapper.insert(goodsSku);

			goodsSkuData.put("minPrice", goodsSku.getPrice());
			goodsSkuData.put("maxPrice", goodsSku.getPrice());
			goodsSkuData.put("minLineationPrice", goodsSku.getLinePrice());
			goodsSkuData.put("maxLineationPrice", goodsSku.getLinePrice());
			goodsSkuData.put("totalStock", goodsSku.getStock());

		} else {
			List<SpecValueVo> specValue = goodsCreateValidate.getSpecValue();
			Map<String, Integer> specListData = new LinkedHashMap<>();
			for (SpecValueVo specValueVo : specValue) {
				GoodsSkuName goodsSkuName = new GoodsSkuName();
				goodsSkuName.setGoodsId(goodsId);
				goodsSkuName.setName(specValueVo.getName());
				goodsSkuNameMapper.insert(goodsSkuName);
				List<SpecListVo> specList = specValueVo.getSpecList();
				for (SpecListVo specListVo : specList) {
					GoodsSkuValue goodsSkuValue = new GoodsSkuValue();
					goodsSkuValue.setGoodsId(goodsId);
					goodsSkuValue.setSkuNameId(goodsSkuName.getId());
					goodsSkuValue.setValue(specListVo.getValue());
					goodsSkuValueMapper.insert(goodsSkuValue);
					specListData.put(specListVo.getValue(), goodsSkuValue.getId());
				}
			}
			List<GoodsSkuVo> serviceSpecValueList = goodsCreateValidate.getServiceSpecValueList();

			List<BigDecimal> priceList = new LinkedList<>();
			List<BigDecimal> lineationPrice = new LinkedList<>();
			Integer totalStock = 0;
			for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
				String skuValueArr = goodsSkuVo.getSkuValueArr();
				String[] skuValueArrList = skuValueArr.split(","+"");
				StringBuilder skuIds = new StringBuilder();
				for (String skuName : skuValueArrList) {
					Integer skuNameId = specListData.get(skuName);
					if (skuIds.toString().equals(""+"")) {
						skuIds = new StringBuilder(skuNameId.toString());
					} else {
						skuIds.append(",").append(skuNameId.toString());
					}
				}

				goodsSkuVo.setSkuValueIds(skuIds.toString());
				goodsSkuVo.setGoodsId(goodsId);
				GoodsSku goodsSku = new GoodsSku();
				BeanUtils.copyProperties(goodsSkuVo, goodsSku);
				goodsSkuMapper.insert(goodsSku);
				priceList.add(goodsSkuVo.getPrice());
				lineationPrice.add(StringUtils.isNull(goodsSkuVo.getLinePrice()) ? new BigDecimal(0) : goodsSkuVo.getLinePrice());
				totalStock += goodsSkuVo.getStock();
			}
			goodsSkuData.put("minPrice", Collections.min(priceList));
			goodsSkuData.put("maxPrice", Collections.max(priceList));
			goodsSkuData.put("minLineationPrice", Collections.min(lineationPrice));
			goodsSkuData.put("maxLineationPrice", Collections.max(lineationPrice));
			goodsSkuData.put("totalStock", totalStock);
		}
		return goodsSkuData;
	}

	/** 
	 * 编辑商品规格
	 *
	 * @author cjhao
	 * @param oldGoodsSpec 旧商品规格
	 * @param goodsUpdateValidate 商品参数
	 */
	private Map<String, Object> __editGoodsSpec(Integer oldGoodsSpec, GoodsUpdateValidate goodsUpdateValidate) {
		Map<String, Object> goodsSkuData = new LinkedHashMap<>();
		List<BigDecimal> priceList = new LinkedList<>();
		List<BigDecimal> lineationPrice = new LinkedList<>();
		Integer totalStock = 0;

		for (GoodsSkuVo goodSkuVo : goodsUpdateValidate.getSpecValueList()) {
			if (goodSkuVo.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
				throw new OperateException("销售价格不能少于等于0");
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getLinePrice())) {
				goodSkuVo.setLinePrice(BigDecimal.ZERO);
			} else {
				if (goodSkuVo.getLinePrice().compareTo(BigDecimal.ZERO) < 0) {
					throw new OperateException("划线价格不能少于0");
				}
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getMarketPrice())) {
				goodSkuVo.setMarketPrice(BigDecimal.ZERO);
			} else {
				if (goodSkuVo.getMarketPrice().compareTo(BigDecimal.ZERO) < 0) {
					throw new OperateException("市场价格不能少于0");
				}
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getStock()) || goodSkuVo.getStock() < 0) {
				throw new OperateException("库存数量不能少于0");
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getWeight())) {
				goodSkuVo.setWeight(0.0);
			} else {
				if (goodSkuVo.getWeight() < 0) {
					throw new OperateException("重量值不能少于0");
				}
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getVolume())) {
				goodSkuVo.setVolume(0.0);
			} else {
				if (goodSkuVo.getVolume() < 0) {
					throw new OperateException("体积值不能少于0");
				}
			}

			if (ObjectUtils.isEmpty(goodSkuVo.getCode())) {
				goodSkuVo.setCode("");
			}
		}

		// 单规格数据保存
		if (1 == goodsUpdateValidate.getSpecType()){
			if(1 == oldGoodsSpec){
				// 单规格 =》单规格
				GoodsSku goodsSku = goodsSkuMapper.selectOne(new QueryWrapper<GoodsSku>().eq("goods_id", goodsUpdateValidate.getId()));
				BeanUtils.copyProperties(goodsUpdateValidate.getSpecValueList().get(0), goodsSku);
				goodsSku.setGoodsId(goodsUpdateValidate.getId());
				goodsSku.setSkuValueArr("默认");
				goodsSku.setImage(UrlUtils.toRelativeUrl(goodsSku.getImage()));
				goodsSkuMapper.updateById(goodsSku);

				priceList.add(goodsSku.getPrice());
				lineationPrice.add(goodsSku.getLinePrice());
				totalStock = goodsSku.getStock();
			} else {
				// 多规格 =》单规格
				Map<String, Object> whereMap = new LinkedHashMap<>();
				whereMap.put("goods_id",goodsUpdateValidate.getId());
				goodsSkuNameMapper.deleteByMap(whereMap);
				goodsSkuValueMapper.deleteByMap(whereMap);
				goodsSkuMapper.deleteByMap(whereMap);
				// 规格名
				GoodsSkuName goodsSkuName = new GoodsSkuName();
				goodsSkuName.setGoodsId(goodsUpdateValidate.getId());
				goodsSkuName.setName("默认");
				goodsSkuNameMapper.insert(goodsSkuName);
				// 规格值
				GoodsSkuValue goodsSkuValue = new GoodsSkuValue();
				goodsSkuValue.setGoodsId(goodsUpdateValidate.getId());
				goodsSkuValue.setValue("默认");
				goodsSkuValue.setSkuNameId(goodsSkuName.getId());
				goodsSkuValueMapper.insert(goodsSkuValue);
				// 规格项
				GoodsSku goodsSku = new GoodsSku();
				BeanUtils.copyProperties(goodsUpdateValidate.getSpecValueList().get(0), goodsSku);
				goodsSku.setGoodsId(goodsUpdateValidate.getId());
				goodsSku.setSkuValueIds(goodsSkuValue.getId().toString());
				goodsSku.setSkuValueArr("默认");
				goodsSku.setImage(UrlUtils.toRelativeUrl(goodsSku.getImage()));
				goodsSkuMapper.insert(goodsSku);

				priceList.add(goodsSku.getPrice());
				lineationPrice.add(goodsSku.getLinePrice());
				totalStock = goodsSku.getStock();
			}
		}

		// 多规格数据保存
		if (1 != goodsUpdateValidate.getSpecType()) {
			// 单规格 => 多规格
			if (1 == oldGoodsSpec) {
				Map<String,Object> whereMap = new LinkedHashMap<>();
				whereMap.put("goods_id",goodsUpdateValidate.getId());
				goodsSkuNameMapper.deleteByMap(whereMap);
				goodsSkuValueMapper.deleteByMap(whereMap);
				goodsSkuMapper.deleteByMap(whereMap);
				List<SpecValueVo> specValue = goodsUpdateValidate.getSpecValue();
				Map<String, Integer> specListData = new LinkedHashMap<>();
				for (SpecValueVo specValueVo : specValue) {
					GoodsSkuName goodsSkuName = new GoodsSkuName();
					goodsSkuName.setGoodsId(goodsUpdateValidate.getId());
					goodsSkuName.setName(specValueVo.getName());
					goodsSkuNameMapper.insert(goodsSkuName);
					List<SpecListVo> specList = specValueVo.getSpecList();
					for (SpecListVo specListVo : specList) {
						GoodsSkuValue goodsSkuValue = new GoodsSkuValue();
						goodsSkuValue.setGoodsId(goodsUpdateValidate.getId());
						goodsSkuValue.setSkuNameId(goodsSkuName.getId());
						goodsSkuValue.setValue(specListVo.getValue());
						goodsSkuValueMapper.insert(goodsSkuValue);
						specListData.put(specListVo.getValue(), goodsSkuValue.getId());
					}
				}

				List<GoodsSkuVo> serviceSpecValueList = goodsUpdateValidate.getSpecValueList();
				for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
					String skuValueArr = goodsSkuVo.getSkuValueArr();
					String[] skuValueArrList = skuValueArr.split(",");
					StringBuilder skuIds = new StringBuilder();
					for (String skuName : skuValueArrList) {
						Integer skuNameId = specListData.get(skuName);
						if (skuIds.toString().equals("")) {
							skuIds = new StringBuilder(skuNameId.toString());
						} else {
							skuIds.append(",").append(skuNameId.toString());
						}
					}
					goodsSkuVo.setSkuValueIds(skuIds.toString());
					goodsSkuVo.setGoodsId(goodsUpdateValidate.getId());
					GoodsSku goodsSku = new GoodsSku();
					BeanUtils.copyProperties(goodsSkuVo, goodsSku);
					goodsSkuMapper.insert(goodsSku);
					priceList.add(goodsSkuVo.getPrice());
					lineationPrice.add(goodsSkuVo.getLinePrice());
					totalStock += goodsSkuVo.getStock();
				}
			} else {
				// 多规格 => 多规格
				goodsUpdateValidate.setServiceSpecValueList(this.__cartesianSpec(goodsUpdateValidate));
				List<SpecValueVo> specValueVoLists = goodsUpdateValidate.getSpecValue();
				List<GoodsSkuName> specNameLists = goodsSkuNameMapper.selectList(new QueryWrapper<GoodsSkuName>().eq("goods_id",goodsUpdateValidate.getId()));
				List<GoodsSkuValue> skuValueLists = goodsSkuValueMapper.selectList(new QueryWrapper<GoodsSkuValue>().eq("goods_id",goodsUpdateValidate.getId()));
				List<GoodsSku> goodsSkuLists = goodsSkuMapper.selectList(new QueryWrapper<GoodsSku>().eq("goods_id",goodsUpdateValidate.getId()));

				List<Integer> goodsSkuIds = new LinkedList<>();
				List<Integer> specValueNameIds = new LinkedList<>();
				List<Integer> goodsSkuValueIds = new LinkedList<>();
				Map<String,Integer> specListData = new LinkedHashMap<>();

				// 获取全部的规格id
				for (GoodsSkuName specNameList : specNameLists) {
					specValueNameIds.add(specNameList.getId());
				}

				// 获取全部的规格值id
				for (GoodsSkuValue skuValueList : skuValueLists) {
					goodsSkuValueIds.add(skuValueList.getId());
				}

				// 获取全部的规格id
				for (GoodsSku goodsSkuList : goodsSkuLists) {
					goodsSkuIds.add(goodsSkuList.getId());
				}

				// 更新规格项信息
				for (SpecValueVo specValueVoList : specValueVoLists) {
					GoodsSkuName goodsSkuName = new GoodsSkuName();
					goodsSkuName.setName(specValueVoList.getName());
					goodsSkuName.setGoodsId(goodsUpdateValidate.getId());
					Integer specValueNameId = specValueVoList.getId();
					if(specValueVoList.getId() != null && specValueVoList.getId() > 0){
						goodsSkuName.setId(specValueVoList.getId());
						goodsSkuNameMapper.updateById(goodsSkuName);
					}else{
						goodsSkuNameMapper.insert(goodsSkuName);
						specValueNameId = goodsSkuName.getId();
					}

					specValueNameIds.remove(specValueNameId);
					List<SpecListVo> specLists = specValueVoList.getSpecList();
					for (SpecListVo specList : specLists) {
						GoodsSkuValue goodsSkuValue = new GoodsSkuValue();
						goodsSkuValue.setSkuNameId(specValueNameId);
						goodsSkuValue.setGoodsId(goodsUpdateValidate.getId());
						goodsSkuValue.setValue(specList.getValue());
						Integer specValueId = specList.getId();
						if(specValueId != null && specValueId > 0){
							goodsSkuValue.setId(specValueId);
							goodsSkuValueMapper.updateById(goodsSkuValue);
							specListData.put(specList.getValue(),specList.getId());
							goodsSkuValueIds.remove(specValueId);
						}else{
							specValueId = goodsSkuValueMapper.insert(goodsSkuValue);
							specListData.put(specList.getValue(), goodsSkuValue.getId());
						}
					}
				}
				// 获取规格全部信息，更新规格信息
				List<GoodsSkuVo> serviceSpecValueList = goodsUpdateValidate.getServiceSpecValueList();
				for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
					Integer goodsSkuId = goodsSkuVo.getId();
					if(goodsSkuId != null && goodsSkuId > 0){
						GoodsSku goodsSku = new GoodsSku();
						BeanUtils.copyProperties(goodsSkuVo, goodsSku);
						goodsSkuMapper.updateById(goodsSku);
					}else{
						String skuValueArr = goodsSkuVo.getSkuValueArr();
						String[] skuValueArrList = skuValueArr.split(",");
						StringBuilder skuIds = new StringBuilder();
						for (String skuName : skuValueArrList) {
							Integer skuNameId = specListData.get(skuName);
							if (!skuIds.toString().equals("")) {
								skuIds.append(",").append(skuNameId.toString());
							} else {
								skuIds = new StringBuilder(skuNameId.toString());
							}
						}
						goodsSkuVo.setSkuValueIds(skuIds.toString());
						goodsSkuVo.setGoodsId(goodsUpdateValidate.getId());
						GoodsSku goodsSku = new GoodsSku();
						BeanUtils.copyProperties(goodsSkuVo, goodsSku);
						goodsSkuId = goodsSkuMapper.insert(goodsSku);
					}
					goodsSkuIds.remove(goodsSkuId);
					priceList.add(goodsSkuVo.getPrice());
					lineationPrice.add(goodsSkuVo.getLinePrice());
					totalStock += goodsSkuVo.getStock();
				}

				if(specValueNameIds.size() > 0){
					goodsSkuNameMapper.deleteBatchIds(specValueNameIds);
				}

				if(goodsSkuValueIds.size() > 0){
					goodsSkuValueMapper.deleteBatchIds(goodsSkuValueIds);
				}

				if(goodsSkuIds.size() > 0){
					goodsSkuMapper.deleteBatchIds(goodsSkuIds);
				}
			}
		}

		goodsSkuData.put("maxPrice", Collections.max(priceList));
		goodsSkuData.put("minPrice", Collections.min(priceList));
		goodsSkuData.put("minLineationPrice", Collections.min(lineationPrice));
		goodsSkuData.put("maxLineationPrice", Collections.max(lineationPrice));
		goodsSkuData.put("totalStock", totalStock);
		return goodsSkuData;
	}

	/**
	 * 检查商品数据
	 *
	 * @author cjhao
	 * @param goodsCreateValidate 参数
	 */
	private void __checkAddGoodsData(GoodsCreateValidate goodsCreateValidate) {
		// 验证商品名称
		Goods goodsName = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("is_delete", 0)
				.eq("name", goodsCreateValidate.getName()).last("limit 1"));

		Assert.isNull(goodsName, "商品名称已存在");

		// 验证商品编码
		Goods goodsCode = goodsMapper.selectOne(
				new QueryWrapper<Goods>()
				.eq("is_delete", 0)
				.eq("code", goodsCreateValidate.getCode()).last("limit 1"));

		Assert.isNull(goodsCode, "商品编码已存在");

		// 验证商品分类
		List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectBatchIds(goodsCreateValidate.getCategoryId());
		if (goodsCategoryList.size() != goodsCreateValidate.getCategoryId().size()) {
			throw new OperateException("分类信息错误，请刷新分类");
		}

		// 验证商品SKU
		List<GoodsSkuVo> serviceSpecValueList = goodsCreateValidate.getServiceSpecValueList();
		for (GoodsSkuVo goodsSkuVo : serviceSpecValueList) {
			if (StringUtils.isNull(goodsSkuVo.getPrice()) || goodsSkuVo.getPrice().compareTo(BigDecimal.ZERO) < 0) {
				throw new OperateException("销售价格不能少于0");
			}
			if (StringUtils.isNull(goodsSkuVo.getStock()) || goodsSkuVo.getStock() < 0) {
				throw new OperateException("库存数不能少于0");
			}
		}
	}

	@Override
	public boolean checkCashbackPrice(GoodsUpdateValidate goodsUpdateValidate) {
		// 判断下单再返是否符合逻辑
		GoodsValidateDto dto = new GoodsValidateDto();
		BeanUtils.copyProperties(goodsUpdateValidate, dto);
		BigDecimal goodsPrice = __getGoodsPrice(dto);
		return __checkIsExchange(goodsPrice,dto);
	}

	/**
	 * 商品审核
	 *
	 * @author fzr
	 * @param id 主键
	 * @param flag 1 通过 2 拒绝
	 */
	@Override
	public void goodsExamine(Integer id, Integer flag,String refuseReason){
		Goods goods = goodsMapper.selectById(id);
		goods.setExamine(flag);
		if(2 == flag) {
			goods.setRefuseReason(refuseReason);
		}else {
			goods.setRefuseReason("");
		}
		goodsMapper.updateById(goods);
	}

	@Override
	public void goodsAgain(Integer id) {
		Goods goods = new Goods();
		goods.setId(id);
		goods.setRefuseReason("");
		goods.setExamine(0);
		goodsMapper.updateById(goods);
	}

	/**
	 * 	获取领券金额
	 * @param goodsPrice
	 * @param id
	 * @return
	 */
	private BigDecimal __getCouponPrice(BigDecimal goodsPrice,Integer id) {
		BigDecimal preferentialPrice = BigDecimal.ZERO;
		List<BigDecimal> priceList = new LinkedList<>();
		priceList.add(BigDecimal.ZERO);
		List<Coupon> list = couponMapper.selectList(new LambdaQueryWrapper<Coupon>().eq(Coupon::getIsDelete, 0).eq(Coupon::getStatus, 2));
		if(CollectionUtil.isNotEmpty(list)) {
			for (Coupon coup : list) {
				if(2 == coup.getSendTotalType() && coup.getSendTotal() <= 0) {
					continue;
				}
				if(2 == coup.getConditionType() && coup.getConditionMoney().compareTo(goodsPrice) < 0) {
					continue;
				}
				if(StringUtils.isNotEmpty(coup.getUseGoodsIds())) {
					if(2 == coup.getUseGoodsType() && !coup.getUseGoodsIds().contains(String.valueOf(id))) {
						continue;
					}else if(3 == coup.getUseGoodsType() && coup.getUseGoodsIds().contains(String.valueOf(id))) {
						continue;
					}
				}
				priceList.add(coup.getMoney());
			}
			preferentialPrice = Collections.max(priceList);
		}
		return preferentialPrice;
	}

	@Override
	public AjaxResult<Object> computeVipCommission(GoodsUpdateValidate goodsUpdateValidate) {
		Goods goods = new Goods();
		// 计算每级能获得的金额
		GoodsValidateDto dto = new GoodsValidateDto();
		BeanUtils.copyProperties(goodsUpdateValidate, dto);
		// 获取商品售价价格
		BigDecimal goodsPrice = __getGoodsPrice(dto);
		// 供货价
		BigDecimal supplyPrice = goodsUpdateValidate.getSupplyPrice();
		BigDecimal preferentialPrice = __getCouponPrice(goodsPrice, goodsUpdateValidate.getId());

		if(supplyPrice.compareTo(goodsPrice) > 0) {
			return AjaxResult.failed("产品供货价不可大于售价");
		}
		if(preferentialPrice.compareTo(goodsPrice) > 0) {
			return AjaxResult.failed("领券优惠金额不可大于售价");
		}
		// 售价 差价
		BigDecimal price = BigDecimal.ZERO;
		if(goodsUpdateValidate.getCommissionType() == 1) {	// 差价分佣
			// 【产品售价-产品供货价-领券优惠金额】
			price = goodsPrice.subtract(supplyPrice).subtract(preferentialPrice);
			if(price.compareTo(BigDecimal.ZERO) <= 0) {
				return AjaxResult.failed("产品售价-产品供货价-领券优惠金额<=0");
			}
		}else if(goodsUpdateValidate.getCommissionType() == 2) {	// 售价分佣
			// 【产品售价*百分比（如5%）-领券优惠金额】
			BigDecimal divide = goodsUpdateValidate.getCommissionRatio().divide(new BigDecimal("100"));
			BigDecimal multiply = goodsPrice.multiply(divide);
			price = multiply.subtract(preferentialPrice);
		}else {
			return AjaxResult.failed("未知分佣类型");
		}
		List<DistributionLevel> list = distributionLevelMapper.selectList(new LambdaQueryWrapper<DistributionLevel>().eq(DistributionLevel::getIsDelete, 0));
		for (DistributionLevel d : list) {
			// 一级
			BigDecimal superiorAgent = d.getSuperiorAgent().divide(new BigDecimal("100"));
			BigDecimal one = price.multiply(superiorAgent);
			// 二级
			BigDecimal supSuperiorAgent = d.getSupSuperiorAgent().divide(new BigDecimal("100"));
			BigDecimal two = price.multiply(supSuperiorAgent);
			// 二级金币 second_ratio
			BigDecimal secondRatio = BigDecimal.valueOf(d.getSecondRatio()).divide(new BigDecimal("100"));
			BigDecimal multiply = price.multiply(secondRatio);
			switch (d.getId()) {
			case 2:
				goods.setLevel1Silver(one);
				goods.setLevel2Silver(two);
				break;
			case 3:
				goods.setLevel1Gold(one);
				goods.setLevel2Gold(two);
				break;
			case 4:
				goods.setLevel1Diamond(one);
				goods.setLevel2Diamond(two);
				goods.setLevel2DiamondGold(multiply);
				break;
			}
		}
		// 产品利润 price
		// 钻石上级+上上级
		BigDecimal cashbackTotal = goods.getLevel1Diamond().add(goods.getLevel2Diamond())
				.add(goods.getLevel2DiamondGold());
		BigDecimal fixedRation = new BigDecimal("1");
		// 返现 钻石上级+上上级+返金币 不可超过 利润的百分之110
		BigDecimal multiply = price.multiply(fixedRation);
		BigDecimal subtract = multiply.subtract(cashbackTotal);
		goods.setGoldCoin(subtract);
		if(subtract.compareTo(BigDecimal.ZERO) < 0) {
			goods.setGoldCoin(BigDecimal.ZERO);
		}
		return AjaxResult.success(goods);
	}

	@Override
	public AjaxResult<Object> computeGoldCoin(GoodsUpdateValidate goodsUpdateValidate) {
		Map<String,Object> map = new HashMap<String, Object>();
		BigDecimal goldCoin = BigDecimal.ZERO;
		if(null == goodsUpdateValidate.getCashbackRatio() || goodsUpdateValidate.getCashbackRatio().compareTo(BigDecimal.ZERO) < 0) {
			map.put("goldCoin", goldCoin);
			return AjaxResult.success(map);
		}
		// 判断下单再返是否符合逻辑
		GoodsValidateDto dto = new GoodsValidateDto();
		BeanUtils.copyProperties(goodsUpdateValidate, dto);
		// 获取商品售价价格
		BigDecimal goodsPrice = __getGoodsPrice(dto);
		// 供货价
		BigDecimal supplyPrice = goodsUpdateValidate.getSupplyPrice();
		BigDecimal preferentialPrice = __getCouponPrice(goodsPrice, goodsUpdateValidate.getId());

		if(supplyPrice.compareTo(goodsPrice) > 0) {
			return AjaxResult.failed("产品供货价不可大于售价");
		}
		if(preferentialPrice.compareTo(goodsPrice) > 0) {
			return AjaxResult.failed("领券优惠金额不可大于售价");
		}
		if(goodsUpdateValidate.getCommissionType() == 1) {	// 差价分佣
			// 【产品售价-产品供货价-领券优惠金额】
			BigDecimal subtract = goodsPrice.subtract(supplyPrice).subtract(preferentialPrice);
			if(subtract.compareTo(BigDecimal.ZERO) <= 0) {
				return AjaxResult.failed("产品售价-产品供货价-领券优惠金额<=0");
			}
			BigDecimal divide = goodsUpdateValidate.getCashbackRatio().divide(new BigDecimal("100"));
			BigDecimal multiply = subtract.multiply(divide);
			goldCoin = multiply.multiply(new BigDecimal("100"));
		}else if(goodsUpdateValidate.getCommissionType() == 2) {	// 售价分佣
			// 【产品售价*百分比（如5%）-领券优惠金额】
			BigDecimal divide = goodsUpdateValidate.getCommissionRatio().divide(new BigDecimal("100"));
			BigDecimal multiply = goodsPrice.multiply(divide);
			goldCoin = multiply.multiply(new BigDecimal("100"));
		}else {
			return AjaxResult.failed("未知分佣类型");
		}
		map.put("goldCoin", goldCoin);
		return AjaxResult.success(map);
	}

	@Override
	public String checkPrice(GoodsUpdateValidate goodsUpdateValidate) {
		if(goodsUpdateValidate.getCommissionType() != 1 && goodsUpdateValidate.getCommissionType() != 2) {
			throw new OperateException("未知分佣类型");
		}
		
		GoodsValidateDto dto = new GoodsValidateDto();
		BeanUtils.copyProperties(goodsUpdateValidate, dto);
		// 一级银牌 <= 一级金牌 <= 一级钻石
		// 二级银牌 <= 二级金牌 <= 二级钻石
		if((goodsUpdateValidate.getLevel1Silver().compareTo(goodsUpdateValidate.getLevel1Gold()) > 0)
				|| (goodsUpdateValidate.getLevel1Gold().compareTo(goodsUpdateValidate.getLevel1Diamond()) > 0)
				|| (goodsUpdateValidate.getLevel2Silver().compareTo(goodsUpdateValidate.getLevel2Gold()) > 0)
				|| (goodsUpdateValidate.getLevel2Gold().compareTo(goodsUpdateValidate.getLevel2Diamond()) > 0)) {
			throw new OperateException("银牌比例<=金牌比例<=钻石比例");
		}
		
		// 产品利润
		BigDecimal profit = BigDecimal.ZERO;
		// 获取产品售价、供货价
		List<GoodsPriceVo> __getGoodsPriceAndMarketPrice = __getGoodsPriceAndMarketPrice(dto);
		for (GoodsPriceVo goodsPriceVo : __getGoodsPriceAndMarketPrice) {
			// 优惠价格
			BigDecimal preferentialPrice = __getCouponPrice(goodsPriceVo.getPrice(), goodsUpdateValidate.getId());
			
			if(preferentialPrice.compareTo(goodsPriceVo.getPrice()) > 0) {
				throw new OperateException("领券优惠金额不可大于售价:["+goodsPriceVo.getSkuValueArr()+"]");
			}
			
			if(goodsUpdateValidate.getCommissionType() == 1) {	// 差价分佣
				// 【产品售价-产品供货价-领券优惠金额】
				profit = goodsPriceVo.getPrice().subtract(goodsPriceVo.getMarketPrice()).subtract(preferentialPrice);
				if(profit.compareTo(BigDecimal.ZERO) <= 0) {
					throw new OperateException("产品售价-产品供货价-领券优惠金额<=0");
				}
				
			}else if(goodsUpdateValidate.getCommissionType() == 2) {	// 售价分佣
				
				// 【产品售价*百分比（如5%）-领券优惠金额】
				BigDecimal divide = goodsUpdateValidate.getCommissionRatio().divide(new BigDecimal("100"));
				BigDecimal multiply = goodsPriceVo.getPrice().multiply(divide);
				profit = multiply.subtract(preferentialPrice);
				if(profit.compareTo(BigDecimal.ZERO) <= 0) {
					throw new OperateException("产品售价-(百分比%)-领券优惠金额<=0");
				}
			}
			BigDecimal fixedRation = new BigDecimal("110");
//			// 返现 钻石上级+上上级+返金币 不可超过 利润的百分之110
//			BigDecimal multiply = profit.multiply(fixedRation);
//			BigDecimal cashbackTotal = goodsUpdateValidate.getLevel1Diamond().add(goodsUpdateValidate.getLevel2Diamond())
//					.add(goodsUpdateValidate.getLevel2DiamondGold()).add(goodsUpdateValidate.getGoldCoin());
//			if(cashbackTotal.compareTo(multiply) > 0) {
//				return "钻石上级("+goodsUpdateValidate.getLevel1Diamond()+") + "+"钻石上上级("+goodsUpdateValidate.getLevel2Diamond().add(goodsUpdateValidate.getLevel2DiamondGold())+") + "
//						+ "返金币("+goodsUpdateValidate.getGoldCoin()+") 必须 <= 利润的110% 即("+multiply+"),请调整";
//			}
			// 以上分佣累加的最高值不得超过110%（例如钻石会员：上级+上上级+下单返金币=总和*100%），否则设置无效
			BigDecimal ration = goodsUpdateValidate.getLevel1Diamond().add(goodsUpdateValidate.getLevel2Diamond()).add(goodsUpdateValidate.getLevel2DiamondGold()
					.add(goodsUpdateValidate.getCashbackRatio()));
			if(ration.compareTo(fixedRation) > 0) {
				return "钻石上级("+goodsUpdateValidate.getLevel1Diamond()+") + "+"钻石上上级("+goodsUpdateValidate.getLevel2Diamond().add(goodsUpdateValidate.getLevel2DiamondGold())+") + "
						+ "返金币("+goodsUpdateValidate.getCashbackRatio()+") 必须 <=110%,现:("+ration+"%),请调整";
			}
		}
		return "分佣合理";
	}

	@Override
	public AjaxResult<Object> getDefaultCommission() {
		List<DistributionLevel> list = distributionLevelMapper.selectList(new LambdaQueryWrapper<DistributionLevel>().eq(DistributionLevel::getIsDelete, 0));
		Goods goods = new Goods();
		for (DistributionLevel distributionLevel : list) {
			if(2 == distributionLevel.getWeights()) {
				goods.setLevel1Silver(distributionLevel.getSuperiorAgent());
				goods.setLevel2Silver(distributionLevel.getSupSuperiorAgent());
			}else if(3 == distributionLevel.getWeights()) {
				goods.setLevel1Gold(distributionLevel.getSuperiorAgent());
				goods.setLevel2Gold(distributionLevel.getSupSuperiorAgent());
				
			}else if(4 == distributionLevel.getWeights()) {
				goods.setLevel1Diamond(distributionLevel.getSuperiorAgent());
				goods.setLevel2Diamond(distributionLevel.getSupSuperiorAgent());
				goods.setLevel2DiamondGold(BigDecimal.valueOf(distributionLevel.getSecondRatio()));
			}
		}
		return AjaxResult.success(goods);
	}

}
